﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;

namespace Heimdallr
{
    public class ServiceElementControllerHub : Collection<ServiceElementController>
    {
        public IEnumerable<ITriggerElement> TriggerElements
        {
            get
            {
                return (from item in base.Items
                        let trigger = item.ElementBase as ITriggerElement
                        where trigger != null
                        select trigger).ToList();
            }
        }

        public IEnumerable<INotifyElement> NotifyElements
        {
            get
            {
                return (from item in base.Items
                       let notify = item.ElementBase as INotifyElement
                       where notify != null
                       select notify).ToList();
            }
        }

        public ServiceElementController Add(ServiceElement element)
        {
            ServiceElementController controller = new ServiceElementController(element);

            base.Add(controller);

            return controller;
        }

        public ServiceElementController<TElement> Add<TElement>(TElement element)
          where TElement : ServiceElement
        {
            ServiceElementController<TElement> controller = new ServiceElementController<TElement>(element);

            base.Add(controller);

            return controller;
        }

        public void StartAll()
        {
            foreach (var controller in base.Items)
                controller.Start();
        }

        public void StopAll()
        {
            foreach (var controller in base.Items)
                controller.Stop();
        } 
    }
}
